package com.blog.controller;

import com.blog.exception.ExceptionEnum;
import com.blog.pojo.Notice;
import com.blog.pojo.PageBean;
import com.blog.pojo.ResultResponse;
import com.blog.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    // 分页查看所有公告
    @GetMapping("/notices")
    public ResultResponse listAll(@RequestParam(defaultValue = "1") Integer page) {
        log.info("分页展示所有公告");
        Integer pageSize = 10;
        PageBean pageBean = noticeService.listAll(page, pageSize);
        return ResultResponse.success(pageBean);
    }

    // 查看单个公告详情
    @GetMapping("/notices/{id}")
    public ResultResponse listById(@PathVariable Integer id) {
        log.info("查看单个公告，公告id: {}", id);
        Notice notice = noticeService.listById(id);
        return ResultResponse.success(notice);
    }


    // 发布公告
    @PostMapping("/notices/create")
    public ResultResponse add(@RequestBody Notice notice) {
        log.info("发布公告");
        if(notice.getTitle() == null) {
            return ResultResponse.error(ExceptionEnum.EMPTY_TITLE);
        }
        if(notice.getContent() == null) {
            return ResultResponse.error(ExceptionEnum.EMPTY_CONTENT);
        }
        noticeService.add(notice);
        return ResultResponse.success();
    }

    // 删除公告
    @DeleteMapping("/notices/delete/{id}")
    public ResultResponse delete(@PathVariable Integer id) {
        log.info("删除公告，公告id: {}", id);
        noticeService.delete(id);
        return ResultResponse.success();
    }

}
